package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdMapper;
import com.qf.pojo.DtsAd;
import com.qf.service.DtsAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class DtsAdServiceImpl implements DtsAdService {
    @Autowired
    DtsAdMapper adMapper;
    @Override
    public IPage<DtsAd> findByPage(Integer page, Integer limit, String sort, String order, String name, String content) {
        IPage<DtsAd> iPage = new Page<>(page,limit);
        QueryWrapper<DtsAd> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            qw.like("name",name);
        }
        if(!StringUtils.isEmpty(content)){
            qw.like("content",content);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        adMapper.selectPage(iPage,qw);

        return iPage;
    }

    @Override
    public DtsAd add(DtsAd dtsAd) {
        dtsAd.setAddTime(new Date());
        dtsAd.setDeleted(false);
        adMapper.insert(dtsAd);
        return dtsAd;
    }

    @Override
    public void update(DtsAd dtsAd) {
        dtsAd.setUpdateTime(new Date());
        dtsAd.setDeleted(false);
        adMapper.updateById(dtsAd);
    }

    @Override
    public void delete(DtsAd dtsAd) {
        adMapper.deleteById(dtsAd);
    }
}
